"""
A group of pieces
"""

from pygo1963.model.Constants import EMPTY
from pygo1963.goLogic.GoUtils import get_vertex_around_position
from pygo1963.model.Vertex import Vertex

class Group:
    "A Group is a group of Go pieces that are together"
    
    # Class members
    # board_size
    # color
    # stones
    # liberties
    
    def __init__(self, board, color, stone = None):
        """
        Creates a group of a given color, and the initial stone
        """
        self.board_size = board.size
        self.color = color
        self.liberties = []
        
        if stone:
            self.add_liberties(board, stone)
            self.stones = [stone]
        else:
            self.stones = []
    
    def copy(self, board):
        """
        Returns a copy of the instance.
        """

        group = Group(board, self.color)
        group.stones = self.stones[:]
        group.liberties = self.liberties[:]
        
        return group
    
    def add_stone(self, board, stone):
        self.stones.append(stone)
        self.add_liberties(board, stone)
    
    def remove_stone(self, stone):
        self.stones.remove(stone)
        #TODO: Falta remove liberties.
        
    def add_liberties(self, board, stone):
        """
        Pre: the move is valid
        Updates the liberties after a stone is added to this group
        """
        if stone in self.liberties:
            self.liberties.remove(stone)

        possible_liberties = get_vertex_around_position(board.size, stone)
        
        for possible_libertie in possible_liberties:
            if possible_libertie not in self.liberties and board.content[possible_libertie.x][possible_libertie.y] == EMPTY:
                self.liberties.append(possible_libertie)
    
    def remove_libertie(self, stone):
        """
        When a stone of the other color is added, check is it has to remove a libertie
        """
        if stone in self.liberties:
            self.liberties.remove(stone)
    
#    def is_captured_after_move(self, move):
#        """
#        Returns true if an opponent move captures this group
#        """
#        if self.liberties == [move.vertex]:
#            return True 
#        
#        return False
    
    def is_captured(self):
        """
        Returns true if the group has been captured
        """
        return not self.liberties

    def is_atari(self):
        """
        Returs true if the group can be captured with one play
        """
        return len(self.liberties) == 1
    
    def __str__(self):
        """
        String representation of the board.
        """
        
        columns = "ABCDEFGHJKLMNOPQRST"
        SPACE = " "

        board = "\n"
        board += SPACE * 3 + SPACE.join(columns[0:self.board_size]) + "\n"
        
        for x in range(self.board_size - 1, -1, -1):
            
            row = str(x + 1)
            
            if len(row) == 1:
                board += SPACE + row + SPACE
            else:
                board += row + SPACE

            for y in range(self.board_size):                  
                content = "."
                if (Vertex(y,x) in self.stones):
                    content = self.color
                elif (Vertex(y,x) in self.liberties):
                    content = "L"

                board += content + SPACE
                
            if self.board_size > 9 and len(row) == 1:
                board += SPACE 

            board +=  row  + "\n"

        board += SPACE * 3 + SPACE.join(columns[0:self.board_size])
        
        return board